layui.define(['admin','table','form','layarea','element'],function (exports){
    let $ = layui.$
        ,admin  = layui.admin
        ,view   = layui.view
        ,table = layui.table
        ,form = layui.form
        ,layarea = layui.layarea
        ,element = layui.element

    let model = 'bin'
        ,module = ['machine','temp','deliver']

    const active = {
        machine_form:function (d){
            admin.popup({
                title: '机器编辑'
                ,area: ['550px', '600px']
                ,id: 'home-machine-table'
                ,resize: false
                ,success: function(layero, index){
                    view(this.id).render(model+'/form/'+module[0]+'_form', d).done(function(){

                        layarea.render({
                            elem: '#city-picker',
                            name: 'name',
                            change: function (res) {
                                //选择结果
                                console.log(res);
                            }
                        });

                        //监听提交
                        form.on('submit(layuiadmin-app-form-submit)', function(data){
                            var field = data.field; //获取提交的字段

                            admin.req({
                                url:'home/machine/post'
                                ,type: 'post'
                                ,data:field
                                ,done:function (res) {

                                    layui.table.reload('LAY-home-machine'); //重载表格
                                    layer.close(index); //执行关闭
                                }
                            });
                            return false;
                        });

                    });
                }
            });
        },

        machine_child_form:function (d){
            admin.popup({
                title: '机器箱柜'
                ,area: ['600px', '600px']
                ,id: 'home-machine-child-table'
                ,resize: false
                ,success: function(layero, index){
                    view(this.id).render(model+'/form/'+module[0]+'_child_form', d).done(function(){

                    });
                }
            });
        },

        machine_password:function(d){
            admin.popup({
                title: '修改密码'
                ,area: ['550px', '300px']
                ,id: 'home-machine-password-table'
                ,resize: false
                ,success: function(layero, index){
                    view(this.id).render(model+'/form/'+module[0]+'_password_form', d).done(function(){

                        //监听提交
                        form.on('submit(layuiadmin-app-form-submit)', function(data){
                            var field = data.field; //获取提交的字段

                            admin.req({
                                url:'home/machine/password'
                                ,type: 'post'
                                ,data:field
                                ,done:function (res) {

                                    layui.table.reload('LAY-home-machine'); //重载表格
                                    layer.close(index); //执行关闭
                                }
                            });
                            return false;
                        });

                    });
                }
            });
        },

        rates_temolate_form:function (d) {

            admin.popup({
                title: '添加模板'
                ,area: ['1000px', '700px']
                ,id: 'bin-temp-form'
                ,resize: false
                ,success: function(layero, index){
                    view(this.id).render(model+'/form/'+module[1]+'_form', d).done(function(){

                        form.render(null, 'layuiadmin-app-form-list');

                        //监听提交
                        form.on('submit(layuiadmin-app-form-submit)', function(data){
                            var field = data.field; //获取提交的字段

                            admin.req({
                                url:model+'/temp/post'
                                ,type: 'post'
                                ,data:field
                                ,done:function (res) {

                                    layui.table.reload('LAY-bin-temp'); //重载表格
                                    layer.close(index); //执行关闭
                                }
                            });
                            return false;
                        });

                    });
                }
            });
        }
    }

    table.render({
        elem: '#LAY-'+model+'-'+module[0]
        ,toolbar: '#table-tool'
        ,url: '/api/admins/'+model+'/'+module[0]
        ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
        ,cols: [[
            {field: 'id', width: 80, title: 'ID'}
            ,{field: 'store_name',title: '商户'}
            ,{field: 'city_name',title: '城市'}
            ,{field: 'address',title: '地址'}
            ,{field: 'is_online', width: 120, title: '在线状态',templet:'#table-status'}
            ,{title: '操作', width: 200, align: 'center', fixed: 'right', toolbar: '#app-table-oper'}
        ]]
        ,page:true
    });

    //头部工具栏
    table.on('toolbar(LAY-'+model+'-'+module[0]+')', function(obj){

        switch(obj.event){
            case 'add':
                active.machine_form();
                break;
        };
    });

    table.on('tool(LAY-'+model+'-'+module[0]+')', function(obj){
        var data = obj.data;
        if(obj.event === 'all'){
            layer.confirm('确定打开所有门？', function(index){

                admin.req({
                    url: 'home/machine/open/door/all'
                    ,data:data
                    ,done:function (res) {

                        layer.close(index);
                    }
                });
            });
        } else if(obj.event === 'edit'){
            active.machine_form(data);
        } else if (obj.event === 'password'){

            active.machine_password(data);
        } else if (obj.event === 'child'){

            active.machine_child_form(data);
        }
    });

    let table_id_1 = model+'-'+module[1]
        ,table_path_1 = model+'/'+module[1]

    table.render({
        elem: '#LAY-'+table_id_1
        ,toolbar: '#table-tool'
        ,url: '/api/admins/'+table_path_1
        ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
        ,cols: [[
            {field: 'id', width: 80, title: 'ID'}
            ,{field: 'name',title: '模板名称'}
            ,{field: 'rates_desc',title: '投递收费', templet:'#table-rates-desc'}
            ,{field: 'user_desc',title: '用户收费', templet: '#table-user-desc'}
            ,{title: '操作', width: 80, align: 'center', fixed: 'right', toolbar: '#app-table-oper'}
        ]]
        ,page:true
    });

    //头部工具栏
    table.on('toolbar(LAY-'+table_id_1+')', function(obj){

        switch(obj.event){
            case 'add':
                active.rates_temolate_form();
                break;
        };
    });

    table.on('tool(LAY-'+table_id_1+')', function(obj){
        var data = obj.data;
        if(obj.event === 'all'){
            layer.confirm('确定打开所有门？', function(index){

                admin.req({
                    url: table_path_1+'/open/door/all'
                    ,data:data
                    ,done:function (res) {

                        layer.close(index);
                    }
                });
            });
        } else if(obj.event === 'edit'){
            active.rates_temolate_form(data);
        } else if (obj.event === 'password'){

            active.machine_password(data);
        } else if (obj.event === 'child'){

            active.machine_child_form(data);
        }
    });

    //投递帐号列表
    let table_id_2 = model+'-'+module[2]
        ,table_path_2 = model+'/'+module[2]

    active.deliver_form = function(d){

        admin.popup({
            title: '添加模板'
            ,area: ['1000px', '700px']
            ,id: 'bin-deliver-form'
            ,resize: false
            ,success: function(layero, index){
                view(this.id).render(model+'/form/'+module[1]+'_form', d).done(function(){

                    form.render(null, 'layuiadmin-app-form-list');

                    //监听提交
                    form.on('submit(layuiadmin-app-form-submit)', function(data){
                        var field = data.field; //获取提交的字段

                        admin.req({
                            url:model+'/temp/post'
                            ,type: 'post'
                            ,data:field
                            ,done:function (res) {

                                layui.table.reload('LAY-bin-temp'); //重载表格
                                layer.close(index); //执行关闭
                            }
                        });
                        return false;
                    });

                });
            }
        });
    }

    table.render({
        elem: '#LAY-'+table_id_2
        ,toolbar: '#table-tool'
        ,url: '/api/admins/'+table_path_2
        ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
        ,cols: [[
            {field: 'id', width: 80, title: 'ID'}
            ,{field: 'name',title: '模板名称'}
            ,{field: 'rates_desc',title: '投递收费', templet:'#table-rates-desc'}
            ,{field: 'user_desc',title: '用户收费', templet: '#table-user-desc'}
            ,{title: '操作', width: 80, align: 'center', fixed: 'right', toolbar: '#app-table-oper'}
        ]]
        ,page:true
    });

    //头部工具栏
    table.on('toolbar(LAY-'+table_id_2+')', function(obj){

        switch(obj.event){
            case 'add':
                active.rates_temolate_form();
                break;
        };
    });

    table.on('tool(LAY-'+table_id_2+')', function(obj){
        var data = obj.data;

        if(obj.event === 'edit'){
            active.rates_temolate_form(data);
        } else if (obj.event === 'password'){

            active.machine_password(data);
        } else if (obj.event === 'child'){

            active.machine_child_form(data);
        }
    });


    exports('bin',active);
});
